package com.zidian.leader.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zidian.leader.R;
import com.zidian.leader.activity.ColleagueEvaTwoIndexActivity;
import com.zidian.leader.adapter.ColleagueEvaAdapter;
import com.zidian.leader.api.Constant;
import com.zidian.leader.common.fragment.BaseFragment;
import com.zidian.leader.common.util.HawkUtils;
import com.zidian.leader.common.util.StringConstants;
import com.zidian.leader.entity.ColleagueEvaResult;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import okhttp3.Call;

/**
 * 督导/同行评价
 * Created by GongCheng on 2016/11/4.
 */

public class ColleagueEvaFragment extends BaseFragment {

    @Bind(R.id.teacher_eva_detail_list_rv)
    RecyclerView teacherEvaDetailListRv;

    private String statisticsType;
    private String collegeName;
    private ColleagueEvaAdapter adapter;

    public static ColleagueEvaFragment newInstance(String statisticsType, @Nullable String collegeName) {
        Bundle args = new Bundle();
        ColleagueEvaFragment fragment = new ColleagueEvaFragment();
        fragment.setArguments(args);
        fragment.statisticsType = statisticsType;
        fragment.collegeName = collegeName;

        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_teacher_eva_detail;
    }

    @Override
    protected void initView() {
        adapter = new ColleagueEvaAdapter();
        teacherEvaDetailListRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        teacherEvaDetailListRv.setAdapter(adapter);
        getColleagueEvaSummary();
        adapter.setOnItemClickListener(new ColleagueEvaAdapter.OnItemClickListener() {
            @Override
            public void onClick(String indexName, float indexScore) {
                Intent intent = new Intent(getContext(), ColleagueEvaTwoIndexActivity.class);
                intent.putExtra("indexName", indexName);
                intent.putExtra("indexScore",indexScore);
                intent.putExtra("collegeName", collegeName);
                intent.putExtra("statisticsType", statisticsType);
                startActivity(intent);
            }
        });
    }

    /**
     * 得到学院评价详情
     */
    public void getColleagueEvaSummary() {
        Map<String, String> map = new HashMap<>();
        map.put("statisticsType" ,"supervisorEvaluate");
        map.put("collegeName",  collegeName);
        map.put("statisticsType", statisticsType);
        map.put("teacherId", HawkUtils.getUsername());
        map.put("roleId", HawkUtils.getRoleId() + StringConstants.STRING_EMPTY);
        map.put("token", HawkUtils.getToken());
        String url = Constant.URL_LEADER + "leadership/teacherEvaluationProfile";
        progressDialog.show();
        OkHttpUtils.getInstance().post().url(url)
                .params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                progressDialog.dismiss();
            }

            @Override
            public void onResponse(String response, int id) {
                progressDialog.dismiss();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    Gson gson = new Gson();
                    ColleagueEvaResult test = gson.fromJson(response, ColleagueEvaResult.class);
                    test.toString();
                    adapter.setList(test.getData());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
//        subscription = HttpFactory.getInstance()
//                .getColleagueEva(statisticsType, collegeName, HawkUtils.getUsername(),
//                        HawkUtils.getRoleId() + StringConstants.STRING_EMPTY, HawkUtils.getToken())
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Subscriber<String>() {
//                    @Override
//                    public void onCompleted() {
//
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//
//                    }
//
//                    @Override
//                    public void onNext(String s) {
//                        Gson gson = new Gson();
//                        try{
//                            ColleagueEvaResult result = gson.fromJson(s, ColleagueEvaResult.class);
//                            result.getData();
//                            adapter.setList(result.getData());
//                        } catch (Throwable e) {
//
//                        }
//
//                    }
//                });
    }

}
